<?php

class StokModel{
	private $db;
	private $subbagian;
	
	private $validColumn;

	private $data;

	private $result;

	private $sort_by;
	private $sort_order;

	private $search_keyword;
	private $search_field;

	private $limit;
	
	function StokModel($dbObject){
		$this->subbagian 	= 'stok';		
		$this->db 			= $dbObject;

		$this->validColumn  = array(
			'tgl_masuk', 
			'label_tandan', 
			'volume', 
			'lokasi'
		);
		
		// settting up configuration
		if(($this->start=$this->getGET('start')) == NULL)
			$this->start = 0;

		$this->sort_by 			= $this->getGET('sort_by');
		$this->sort_order 		= $this->getGET('sort_order');
		$this->search_keyword	= $this->getGET('search_keyword');
		$this->search_field		= $this->getGET('search_field');

		if(($this->limit=$this->getGET('limit')) == NULL)
			$this->limit = 50;		
	}
	
	private function getGET($param){
		if( isset( $_GET[$param] ) && $_GET[$param] != '' )
			return $_GET[$param];
		else
			return NULL;
	}
	
	public function countAll(){
		$where = "";
		if( $this->search_keyword && $this->search_field )
			$where = "WHERE ".$this->search_field."='".$this->search_keyword."'";
			
		$q = "SELECT COUNT(*) FROM `caramel_managerial`.`stok` $where";
		
		$this->total_record = $this->db->get_var($q);
		
		return $this->total_record;
	}	
	
	public function search(){			
		$where = "";
		if( $this->search_keyword && $this->search_field )
			$where = "WHERE ".$this->search_field."='".$this->search_keyword."'";

		// get data
		if( $this->sort_by )
			$q = "
			SELECT `id`, `tgl_masuk`, `label_tandan`, `volume`, `lokasi`
			FROM `caramel_managerial`.`stok` $where
			ORDER BY ".$this->sort_by." ".$this->sort_order." LIMIT ".$this->start.",".$this->limit."";
		else
			$q = "
			SELECT `id`, `tgl_masuk`, `label_tandan`, `volume`, `lokasi` 
			FROM `caramel_managerial`.`stok` $where ORDER BY `tgl_masuk` DESC
			LIMIT ".$this->start.",".$this->limit."";

		$this->result = $this->db->get_results($q, ARRAY_A);
		return $this->result;
	}

	public function getResult(){
		return $this->result;
	}

	function format($data){
		$result = array();
		foreach($this->validColumn as $v){
			if( isset($data[$v]) )
				$result[$v] = $data[$v];
			else
				$result[$v] = NULL;
		}
		
		return $result;
	}
	
	private function insertStok(){
		$dataStok   = array();
		$stokColumn = array(
			'tgl_masuk', 
			'label_tandan', 
			'volume', 
			'lokasi');
		
		// copy dari $data ke $dataStok
		foreach($stokColumn as $c){
			$dataStok[$c] = $this->data[$c];
		}
		
		$qHeader = "(`".implode($stokColumn, "`, `")."`)";
		$qBody   = "('".implode($dataStok, "', '")."')";
		
		$query = "INSERT INTO `caramel_managerial`.`stok` ".$qHeader." VALUES ".$qBody;
		$this->db->query($query);
	}	
	
	function put($data){		
		$this->data	= $this->format($data);// buang field2 yang tidak dibutuhkan
		
		if( $this->isExist() == FALSE ){				
			// stok
			$this->insertStok();					
		}
		else{
			return INSERT_FAIL_DUPLICATE;
		}		
	}
	
	function isExist(){
		$query = "SELECT COUNT(*) FROM `caramel_managerial`.`stok` WHERE `label_tandan`='".$this->data['label_tandan']."'";
		$count = $this->db->get_var($query);
		
		if( $count > 0 )
			return TRUE;
			
		return FALSE; // tidak ada data yang dicari... no duplicate		
	}
    
    public function delete($id){
        $q[] = "
        DELETE FROM `caramel_managerial`.`stok` 
        WHERE `id`='$id'";
        
        foreach($q as $query){
            $this->db->query($query);
        }
    }
}
?>